/*
 * Copyright (c) 2011 Metrum Research Group LLC. All Rights Reserved.
 *
 * You may use this software under the terms of GPL v3 (http://www.gnu.org/licenses/gpl.html).
 *
 * METRUM RESEARCH GROUP LLC MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. METRUM RESEARCH GROUP LLC SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package sumo;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.io.Util;

class IOUtil{
	private static byte[] ERROR_BYTES = "There was an unexpected exception. The session was closed ".getBytes();
	public IOUtil(){
		super();
	}
	
	public void readWrite
		(final InputStream remoteInput, final OutputStream localOutput) {
	             	
		Thread writer = new Thread(){
			public void run(){
				try{
					Util.copyStream(remoteInput, localOutput);
					yield();
				} catch (IOException e){
					//e.printStackTrace();
					// Set appropriate error message
					// and exit the thread
					try{
						localOutput.write(ERROR_BYTES);	
					}catch(Exception e2){ 
						System.out.println("IOUtil run exception: " + e2.getMessage());
					}finally{
					}				
				}			
			}};
		writer.setPriority(Thread.currentThread().getPriority() + 1);
		writer.start();
	}
}

public class SumoUtils {
	private SumoUtils() {
	}

	public static String getFileExtension(String filename) {

		String ext = null;
		int dotPlace = filename.lastIndexOf('.');

		if (dotPlace >= 0) {
			ext = filename.substring(dotPlace + 1);
		}

		return ext;
	}

	public static String fixRInput(String inputStr) {

		if (inputStr == null) {
			inputStr = System.getProperty("line.separator");
		} else if (!inputStr.endsWith(System.getProperty("line.separator"))) {
			inputStr = inputStr + System.getProperty("line.separator");
		}

		return inputStr;
	}

	public static String fixROutput(String theString) {
		String[] codes = SumoPropertyManager.getConfig().getStringArray("sessionpage.filter");
		for (String code: codes)theString =  theString.replaceAll(code,"");
		return theString;
	}
	public static boolean isFloat(String s){
	    try{
    	    	    Float.valueOf(s).floatValue();
    	    }catch(NumberFormatException nfe){
    	    	    return(false);
    	    }
    	    return true;
    }
}
